﻿<!--#include file="h.asp"--><%
'
'	七色虎建站系统
'	好友页面
'	2011.11.2

Sub main
	Dim id,rs,sql,count,a,userid,body_,now_,state,msg,sort,tip,time_
	checkLogin()'验证登录
	hu.title = "好友页面"
	body_ = checkMsg()'信息
	body_ = body_ & "[div=nav]-"&hu.url("?aid=index","首页")&"-"&hu.url("bbs_user.asp","会员中心")&"-"
	id=getN("id", 0)
	a=getN("a",0)
	sort=getData("sort")
	userid=user_array(0)
	If id=0 Or id=userid Then
		Dim pagesize,page,pagecount,i
		page=getN("page",1)
		if page<=0 then page=1
		pagesize=10
		Select Case a
			Case 0:'我的好友列表
				body_ = body_ & "我的好友[/div]"
				'好友排序，按在线，好友关系
				If sort="" Then
					sql = "select f.fid,u.name,u.remark from (74hu_friend f inner join 74hu_user u on f.fid=u.id) where f.uid="&userid&_
						" and f.state=1 order by f.id asc"
					' count = hu.conn("select count(id) from 74hu_friend where uid="&userid&" and state=1")(0)
					tip = hu.url("bbs_friend.asp?sort=online","查看在线好友") & "<br/>"
				Else
					time_ = dateadd("n", -20, time_now)
					sql = "select f.fid,u.name,u.remark from (74hu_friend f inner join 74hu_user u on f.fid=u.id) where f.uid="&userid&_
						" and f.state=1 and (u.logtime>#"& time_ &"#) order by u.logtime desc"
					tip = hu.url("bbs_friend.asp","查看全部好友") & "<br/>"
				End If
				Set rs = Server.CreateObject("ADODB.Recordset")
				rs.open sql, hu.db, 1, 1
				If Not rs.eof Then
					count = rs.recordcount
					body_ = body_ & "共"&count&"名好友 " & tip
					pagecount=(count+pagesize-1)\pagesize
					if page>pagecount then page=pagecount
					rs.move(pagesize*(page-1))
					For i=1 To PageSize
						If rs.eof Then Exit For
						id =rs("fid")
						body_ = body_ & "[div=odd]"&hu.url("bbs_user.asp?id="&id, nowml(rs("name"))) &"<br/>"& getUserRemark(rs("remark")) & "[/div]"&_
							"[div=even]"&showImg("msgto")&hu.url("bbs_msg.asp?id="&id,"发信息")&" "&showImg("edit")&_
							hu.url("bbs_friend.asp?id="&id&"&amp;a=11","管理好友")&"[/div]"
						rs.movenext
					Next
					body_ = body_ & showPage("bbs_friend.asp", "", page, pagecount)&""
				Else
					body_ = body_ & "暂时没有好友<br/>"
				End If
				rs.close
				Set rs=Nothing
			Case 1:'好友请求
				body_ = body_ & "好友请求[/div]"
				sql = "select f.fid,u.name,u.remark from 74hu_friend f inner join 74hu_user u on f.fid=u.id where f.uid="&userid&" and f.state=0 order by f.id"
				Set rs = Server.CreateObject("ADODB.Recordset")
				rs.open sql, hu.db, 1, 1
				If Not rs.eof Then
					count = rs.recordcount
					body_ = body_ & "共"&count&"条好友请求<br/>"
					pagecount=(count+pagesize-1)\pagesize
					if page>pagecount then page=pagecount
					rs.move(pagesize*(page-1))
					For i=1 To PageSize
						If rs.eof Then Exit For
						id =rs("fid")
						body_ = body_ & "[div=odd]"&hu.url("bbs_user.asp?id="&id, nowml(rs("name")) )&"<br/>"& getUserRemark(rs("remark")) & "[/div]"&_
							"[div=even]"&showImg("edit")&hu.url("bbs_friend.asp?id="&id&"&amp;a=1","同意请求")&" "&_
							hu.url("bbs_friend.asp?id="&id&"&amp;a=3","忽略")&" "&hu.url("bbs_friend.asp?id="&id&"&amp;a=2","拒绝此人")&"[/div]"
						rs.movenext
					Next
					body_ = body_ & showPage("bbs_friend.asp", "a:1", page, pagecount)&""
				Else
					body_ = body_ & "暂时没有好友请求<br/>"
				End If
				rs.close
				Set rs=Nothing
			Case 2:'黑名单
				body_ = body_ & "黑名单[/div]"
				sql = "select f.fid,u.name,u.remark from 74hu_friend f inner join 74hu_user u on f.fid=u.id where f.uid="&userid&" and f.state=2"
				Set rs = Server.CreateObject("ADODB.Recordset")
				rs.open sql, hu.db, 1, 1
				If Not rs.eof Then
					count = rs.recordcount
					body_ = body_ & "共"&count&"名好友<br/>"
					pagecount=(count+pagesize-1)\pagesize
					if page>pagecount then page=pagecount
					rs.move(pagesize*(page-1))
					For i=1 To PageSize
						If rs.eof Then Exit For
						id =rs("fid")
						body_ = body_ & "[div=odd]"&hu.url("bbs_user.asp?id="&id, nowml(rs("name"))) &"<br/>"& getUserRemark(rs("remark")) & "[/div]"&_
							"[div=even]"&showImg("edit")&hu.url("bbs_friend.asp?id="&id&"&amp;a=3","取消加黑")&"[/div]"
						rs.movenext
					Next
					body_ = body_ & showPage("bbs_friend.asp", "a:2", page, pagecount)&""
				Else
					body_ = body_ & "暂时没有黑名单<br/>"
				End If
				rs.close
				Set rs=Nothing
		End Select
		body_ = body_ & "[div=nav]※功能面板[/div]"&hu.url("bbs_friend.asp","我的好友")&" "&hu.url("bbs_friend.asp?a=1","好友请求")&" "&_
			hu.url("bbs_friend.asp?a=2","黑名单")
	Else'好友页面
		now_=time_now
		If id=userid Then rupt "错误", "不能加自己为好友!"
		count=hu.conn("select count(id) from 74hu_user where id="&id)(0)
		If count=0 Then rupt "错误", "没有这个会员!"
		Select Case a
		Case 0:
			body_ = body_ & "添加好友[/div]"
			state= getFriendship(id)'以对方信息为准
			Select Case state
				Case 0: body_ = body_ & "已提交好友申请,等待对方审核<br/>"
				Case 1: body_ = body_ & "他(她)是你的好友,可直接"&hu.url("bbs_msg.asp?id="&id,"发信息")&"<br/>"
				Case 2: body_ = body_ & "你已被拒绝,请不要再打扰他(她)<br/>"
				Case -1: body_ = body_ & hu.url("bbs_friend.asp?id="&id&"&amp;a=10","提出好友申请")&"<br/>"
			End Select
		Case 10:
			body_ = body_ & "添加好友[/div]"
			count=hu.conn("select count(id) from 74hu_friend where uid="&id&" and fid="&userid)(0)'以对方信息为准
			If count>0 Then
				body_ = body_ & "你已提交过申请,请不要重复提交,"
			Else
				count=hu.conn("select count(id) from 74hu_friend where uid="&userid&" and fid="&id)(0)
				If count>0 Then
					'判断是否加了对方黑名单
					hu.conn("update 74hu_friend set state=-1 where uid="&userid&" and fid="&id)
				Else
					hu.conn("insert into 74hu_friend (uid,fid,state) values("&userid&","&id&",-1)")
				End If
				hu.conn("insert into 74hu_friend (fid,uid,state) values("&userid&","&id&",0)")
				body_ = body_ & "你已成功提交好友申请,等待对方审核,"
			End If
		Case 11:'管理好友
			body_ = body_ & "管理好友[/div]"&_
				"[div=tip]知己难求,请谨慎操作[/div]"&_
				"[div=odd]"&hu.url("bbs_friend.asp?id="&id&"&amp;a=3","删除好友")&"[/div]"&_
				"[div=even]"&hu.url("bbs_friend.asp?id="&id&"&amp;a=2","加黑好友")&"[/div]"
		Case 1:'同意好友
			body_ = body_ & "好友请求[/div]"
			sql = "select top 1 state,retime from 74hu_friend where uid="&id&" and fid="&userid&" and state=-1"'避免修改对方的黑名单
			Set rs=hu.conn(sql)
			If Not rs.eof Then
				hu.conn("update 74hu_friend set state=1,retime='"&now_&"' where uid="&id&" and fid="&userid&"")
				Set rs=hu.conn("select top 1 state,retime from 74hu_friend where uid="&userid&" and fid="&id&"")
				If Not rs.eof Then
					hu.conn("update 74hu_friend set state=1,retime='"&now_&"' where uid="&userid&" and fid="&id&"")
				Else
					'不存在好友关系则添加
					hu.conn("insert into 74hu_friend (uid,fid,state,retime) values("&userid&","&id&",1,'"&now_&"')")
				End If
				'同意好友要发通知告诉对方
				msg = ""&user_array(1)&"(ID "&userid&") 已同意你的好友请求"
				Call sendMsgToUserBySystem(id, msg)
				body_ = body_ & "已同意请求,"
			Else
				body_ = body_ & "已没有好友请求,"
			End If
			rs.close
			Set rs=Nothing
		Case 2:'加黑名单
			body_ = body_ & "黑名单[/div]"
			sql = "select top 1 state,retime from 74hu_friend where uid="&userid&" and fid="&id&""
			Set rs=hu.conn(sql)
			If Not rs.eof Then
				hu.conn("update 74hu_friend set state=2,retime='"&now_&"' where uid="&userid&" and fid="&id&"")
			Else
				'不存在好友关系则添加
				hu.conn("insert into 74hu_friend (uid,fid,retime,state) values("&userid&","&id&",'"&now_&"',2)")
			End If
			rs.close
			Set rs=Nothing
			hu.conn("delete from 74hu_friend where uid="&id&" and fid="&userid&" and state<>2")'避免修改对方的黑名单
			body_ = body_ & "已加对方黑名单!<br/>"
		Case 3:'删除好友
			body_ = body_ & "删除好友[/div]"
			sql = "select top 1 id from 74hu_friend where uid="&id&" and fid="&userid&" and state=2"'避免修改对方的黑名单
			Set rs=hu.conn(sql)
			If rs.eof Then
				hu.conn("delete from 74hu_friend where uid="&id&" and fid="&userid&"")
			End If
			rs.close
			Set rs=Nothing
			hu.conn("delete from 74hu_friend where uid="&userid&" and fid="&id&"")
			body_ = body_ & "删除成功!<br/>"
		' Case 20:'取消加黑 改成 直接删除记录
			' body_ = body_ & "黑名单[/div]"
			' Set rs=hu.conn("select state from 74hu_friend where uid="&userid&" and fid="&id&" and state=2")
			' If Not rs.eof Then
				' state=2
				' rs("retime")=now_
				' body_ = body_ & "取消黑名单成功!<br/>"
			' Else
				' body_ = body_ & "你没有把对方加进黑名单!<br/>"
			' End If
		End Select
		body_ = body_ & hu.url("bbs_user.asp?id="&id,"返回他(她)的页面")&"<br/>"& hu.url("bbs_friend.asp","我的好友列表")&"<br/>"
	End If
	hu.body = comUbb(body_)
End Sub

%>